package com.mrd.shiptask_shipowner.serviecs;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.mrd.shiptask_shipowner.LoginActivity;
import com.mrd.shiptask_shipowner.activity.ShipOrderActivity;
import com.mrd.shiptask_shipowner.constants.HttpConstant;
import com.mrd.shiptask_shipowner.constants.database.DBUtil;
import com.mrd.shiptask_shipowner.shiareutil.SharePreferenceUtil;
import com.mrd.shiptask_shipowner.unit_tools.DatasTools;
import com.mrd.shiptask_shipowner.unit_tools.FunctionOfActivity;
import com.mrd.shiptask_shipowner.unit_tools.HttpUrlConstant;
import com.mrd.shiptask_shipowner.unit_tools.HttpUtil;

/**
 * 用户验证服务工具包
 * 
 */
public class BizNetworkService implements Runnable {
	Handler handler;
	int opt_tag;
	Map<String, Object> params;
	Context ctx;
	String errmessage = "";
	boolean checkPassOrNot = false;

	public BizNetworkService(Context ctx, Handler handler, int tag,
			Map<String, Object> params) {
		this.ctx = ctx;
		this.handler = handler;
		this.opt_tag = tag;
		this.params = (params == null ? new HashMap<String, Object>() : params);
	}

	@Override
	public void run() {
		Message msg = new Message();
		Bundle data = new Bundle();
		msg.what = opt_tag;
		String result = "";
		SharePreferenceUtil sp = new SharePreferenceUtil(ctx, "saveUser");
		params.put("deviceuuid", FunctionOfActivity.getDeviceId(ctx));
		switch (opt_tag) {
		// 登录
		case HttpUrlConstant.LOGIN:
			try {
				result = HttpUtil.login(params.get("account").toString(),
						params.get("password").toString(), ctx);
				if (!HttpConstant.MATCH_PASS.equals(result)) {
					data.putString("msg", result);
					data.putBoolean("result", false);
				} else {
					data.putString("msg", "登录完成");
					data.putBoolean("result", true);
				}
				msg.setData(data);
			} catch (Exception e) {
				data.putString("msg", result);
				data.putBoolean("result", false);
				msg.setData(data);
			}
			break;
		// 同步船源列表
		case HttpUrlConstant.INIT_SHIP_ORDER_LIST:
			try {
				Map<String, Object> params2 = new HashMap<String, Object>();
				params2.put("ship_id", sp.getShip_id());
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.INIT_SHIP_ORDER_LIST_URL
								+ "?", params2, ctx).toString();
				JSONArray jas = new JSONArray(result.toString());
				if (!"[]".equals(jas.toString())) {
					DBUtil.deleteShipOrderTableByStatus(ctx, "1", "respond");
					for (int i = 0; i < jas.length(); i++) {
						String getId = DatasTools.getJsonValue(
								jas.getJSONObject(i), "id");
						String getStartPort = DatasTools.getJsonValue(
								jas.getJSONObject(i), "startPort");
						String getEndPort = DatasTools.getJsonValue(
								jas.getJSONObject(i), "endPort");
						String getEShipPort = DatasTools.getJsonValue(
								jas.getJSONObject(i), "eShipPort");
						String getEShipTime = DatasTools.getJsonValue(
								jas.getJSONObject(i), "eShipTime");
						String getEndTime = DatasTools.getJsonValue(
								jas.getJSONObject(i), "endTime");
						String getValid = DatasTools.getJsonValue(
								jas.getJSONObject(i), "valid");
						String getYanQi = DatasTools.getJsonValue(
								jas.getJSONObject(i), "yanQi");
						String getLoadton = DatasTools.getJsonValue(
								jas.getJSONObject(i), "loadton");
						String getGoodsType = DatasTools.getJsonValue(
								jas.getJSONObject(i), "goodsType");
						String getMinPice = DatasTools.getJsonValue(
								jas.getJSONObject(i), "lPrice");
						String getMaxPice = DatasTools.getJsonValue(
								jas.getJSONObject(i), "hPrice");
						DBUtil.insertIntoTableForSave(ctx, sp.getShip_id(),
								"1", getEShipPort, getEShipTime, getLoadton,
								getGoodsType, getStartPort, getEndPort,
								getEndTime, getId, getValid, getYanQi,
								getMinPice, getMaxPice);
					}
					ctx.startActivity(new Intent(ctx, ShipOrderActivity.class));
					data.putString("msg", "ispass");
				} else {
					data.putString("msg", "暂无信息");
					ctx.startActivity(new Intent(ctx, ShipOrderActivity.class));
				}
			} catch (Exception e) {
				ctx.startActivity(new Intent(ctx, ShipOrderActivity.class));
				// TODO: handle exception
				data.putString("msg", result);
			}
			msg.setData(data);
			break;
		// 同步港口信息
		case HttpUrlConstant.INIT_PORTS_JSON:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.INIT_PORTS_JSON_URL, params,
						ctx).toString();
				System.out.println(result);
				JSONArray joa = new JSONArray(result);
				if (joa.length() > 0) {
					DBUtil.deleteAll(ctx, "PORTS");
					for (int i = 0; i < joa.length(); i++) {
						String getPingYin = joa.getJSONObject(i).has("pingYin") == false ? ""
								: joa.getJSONObject(i).get("pingYin")
										.toString();
						String getPort = joa.getJSONObject(i).has("port") == false ? ""
								: joa.getJSONObject(i).get("port").toString();
						DBUtil.insertIntoTableAboutPorts(ctx, getPingYin,
								getPort);
					}
				}
				data.putString("pass", "pass");
			} catch (Exception e) {
				// TODO: handle exception
			}
			msg.setData(data);
			break;
		// 提交船源
		case HttpUrlConstant.SHIP_ORDER:
			try {
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.BASE_URL
								+ HttpUrlConstant.SHIP_ORDER_URL, params, ctx)
						.toString();
				if (!"".equals(result) && null != result) {
					JSONObject jsonResult = new JSONObject(result);
					if (jsonResult.getInt("rs") == 1) {
						errmessage = "发布船源成功";
						checkPassOrNot = true;
						// 删除该编辑
						DBUtil.deleteShipOrderTableByStatus(ctx, "0", params
								.get("taskId").toString());
					} else if (jsonResult.getInt("rs") == 0) {
						errmessage = DatasTools.getJsonValue(jsonResult, "msg");
					}
				} else if ("".equals(result) || null == result) {
					errmessage = "返回空，发布船源失败";
				} else {
					errmessage = "异常，发布船源失败\n" + result;
				}
			} catch (Exception e) {
				// TODO: handle exception
				errmessage = result;
			}
			data.putBoolean("isPassOrNot", checkPassOrNot);
			data.putString("msg", errmessage);
			msg.setData(data);
			break;
		// 提交船源的延期
		case HttpUrlConstant.SUBMIT_YAN_QI:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.SUBMIT_YAN_QI_URL, params,
						ctx).toString();
				System.out.println(result);
				JSONObject joResult = new JSONObject(result.toString());
				if (joResult.has("rs")
						&& "true".equals(joResult.getString("rs"))) {
					data.putString("submit_result", "isPass");
					data.putString("followId", params.get("demandId")
							.toString());
					data.putString(
							"msg",
							joResult.has("msg") == false ? "延期提交成功" : joResult
									.getString("msg"));
				} else {
					data.putString("submit_result", "noPass");
					data.putString(
							"msg",
							joResult.has("msg") == false ? "延期提交失败" : joResult
									.getString("msg"));
				}

			} catch (Exception e) {
				// TODO: handle exception
				data.putString("submit_result", "noPass");
				data.putString("msg", "延期提交异常");
			}
			msg.setData(data);
			break;
		// 提交船源配货
		case HttpUrlConstant.SUBMIT_YI_PEI_HUO:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.SUBMIT_YI_PEI_HUO_URL,
						params, ctx).toString();
				System.out.println(result);
				JSONObject joResult = new JSONObject(result.toString());
				if (joResult.has("rs")
						&& "true".equals(joResult.getString("rs"))) {
					data.putString("submit_result", "isPass");
					data.putString("followId", params.get("demandId")
							.toString());
					data.putString(
							"msg",
							joResult.has("msg") == false ? "已配货提交成功" : joResult
									.getString("msg"));
				} else {
					data.putString("submit_result", "noPass");
					data.putString(
							"msg",
							joResult.has("msg") == false ? "已配货提交失败" : joResult
									.getString("msg"));
				}

			} catch (Exception e) {
				// TODO: handle exception
				data.putString("submit_result", "noPass");
				data.putString("msg", "已配货提交异常");
			}
			msg.setData(data);
			break;
		// 获取货运申请信息
		case HttpUrlConstant.MAKE_TO_ORDER:
			try {
				Map<String, Object> params2 = new HashMap<String, Object>();
				params2.put("ship_id", sp.getShip_id());
				params2.put("page", 1);
				params2.put("size", 20);
				String url = HttpUrlConstant.SHIPBIZ_BASE_URL
						+ HttpUrlConstant.MAKE_TO_ORDER_URL + "?";
				result = HttpUtil.queryStringForPost(url, params2, ctx)
						.toString();
				JSONObject obj = new JSONObject(result);
				JSONArray ary = obj.getJSONArray("content");
				if (ary.length() > 0) {
					data.putString("result", result);
					data.putString("submit_result", "isPass");
					data.putString("msg", "完成获取信息");
				} else {
					data.putString("submit_result", "noPass");
					data.putString("msg", "暂无货运申请信息");
				}
			} catch (Exception e) {
				// TODO: handle exception
				data.putString("submit_result", "noPass");
				data.putString("msg", "获取货运申请信息异常");
			}
			msg.setData(data);
			break;
		// 接受货运申请
		case HttpUrlConstant.AGREE_ORDER:
			try {
				String url = HttpUrlConstant.SHIPBIZ_BASE_URL
						+ HttpUrlConstant.AGREE_ORDER_URL + "?";
				result = HttpUtil.queryStringForPost(url, params, ctx)
						.toString();
				if (!"".equals(result) && null != result) {
					JSONObject jsonResult = new JSONObject(result);
					if (jsonResult.getInt("rs") == 1) {
						errmessage = "货运申请接单成功";
						checkPassOrNot = true;
					} else if (jsonResult.getInt("rs") == 0) {
						errmessage = DatasTools.getJsonValue(jsonResult, "msg");
					}
				} else if ("".equals(result) || null == result) {
					errmessage = "接受货运申请失败";
				} else {
					errmessage = "接受货运申请失败\n" + result;
				}
			} catch (Exception e) {
				// TODO: handle exception
				errmessage = result;
			}
			data.putString("msg", errmessage);
			data.putBoolean("isPassOrNot", checkPassOrNot);
			msg.setData(data);
			break;
		// 拒绝货运申请
		case HttpUrlConstant.DISAGREE_ORDER:
			try {
				String url = HttpUrlConstant.SHIPBIZ_BASE_URL
						+ HttpUrlConstant.DISAGREE_ORDER_URL + "?";
				result = HttpUtil.queryStringForPost(url, params, ctx)
						.toString();
				if (!"".equals(result) && null != result) {
					JSONObject jsonResult = new JSONObject(result);
					if (jsonResult.getInt("rs") == 1) {
						errmessage = "货运申请拒绝成功";
						checkPassOrNot = true;
					} else if (jsonResult.getInt("rs") == 0) {
						errmessage = DatasTools.getJsonValue(jsonResult, "msg");
					}
				} else if ("".equals(result) || null == result) {
					errmessage = "拒绝货运申请失败";
				} else {
					errmessage = "拒绝货运申请失败\n" + result;
				}
			} catch (Exception e) {
				// TODO: handle exception
				data.putString("msg", "拒绝货运申请异常");
			}
			data.putString("msg", errmessage);
			data.putBoolean("isPassOrNot", checkPassOrNot);
			msg.setData(data);
			break;
		// 获取运输合同信息
		case HttpUrlConstant.ElECTRON_MANIFEST_LIST:
			try {
				Map<String, Object> params2 = new HashMap<String, Object>();
				params2.put("ship_id", sp.getShip_id());
				params2.put("page", 1);
				params2.put("size", 30);
				String url = HttpUrlConstant.SHIPBIZ_BASE_URL
						+ HttpUrlConstant.ElECTRON_MANIFEST_LIST_URL + "?";
				result = HttpUtil.queryStringForPost(url, params2, ctx)
						.toString();
				JSONObject obj = new JSONObject(result);
				JSONArray ary = obj.getJSONArray("content");
				if (ary.length() > 0) {
					data.putString("result", result);
					data.putString("submit_result", "isPass");
					data.putString("msg", "完成获取信息");
				} else {
					data.putString("submit_result", "noPass");
					data.putString("msg", "暂无运输合同信息");
				}
			} catch (Exception e) {
				// TODO: handle exception
				data.putString("submit_result", "noPass");
				data.putString("msg", "获取运输合同信息异常");
			}
			msg.setData(data);
			break;
		// 同意合同信息
		case HttpUrlConstant.AGREE_ELECTRO:
			try {
				System.out.println(params);
				String url = HttpUrlConstant.SHIPBIZ_BASE_URL
						+ HttpUrlConstant.AGREE_ELECTRO_URL + "?";
				result = HttpUtil.queryStringForPost(url, params, ctx)
						.toString();
				if (!"".equals(result) && null != result) {
					JSONObject jsonResult = new JSONObject(result);
					if (jsonResult.getInt("rs") == 1) {
						if (jsonResult.has("msg")
								&& !"".equals(DatasTools.getJsonValue(
										jsonResult, "msg"))) {
							errmessage = DatasTools.getJsonValue(jsonResult,
									"msg");
						} else {
							errmessage = "同意运输合同成功";
						}
						checkPassOrNot = true;
					} else if (jsonResult.getInt("rs") == 0) {
						errmessage = DatasTools.getJsonValue(jsonResult, "msg");
					}
				} else if ("".equals(result) || null == result) {
					errmessage = "同意运输合同失败";
				} else {
					errmessage = "同意运输合同失败\n" + result;
				}
			} catch (Exception e) {
				// TODO: handle exception
				errmessage = "同意运输合同异常";
			}
			data.putString("msg", errmessage);
			data.putBoolean("isPassOrNot", checkPassOrNot);
			msg.setData(data);
			break;
		// 不同意合同
		case HttpUrlConstant.DISAGREE_ELECTRO:
			try {
				System.out.println(params);
				String url = HttpUrlConstant.SHIPBIZ_BASE_URL
						+ HttpUrlConstant.DISAGREE_ELECTRO_URL + "?";
				result = HttpUtil.queryStringForPost(url, params, ctx)
						.toString();
				if (!"".equals(result) && null != result) {
					JSONObject jsonResult = new JSONObject(result);
					if (jsonResult.getInt("rs") == 1) {
						errmessage = "不同意运输合同成功";
						checkPassOrNot = true;
					} else if (jsonResult.getInt("rs") == 0) {
						errmessage = DatasTools.getJsonValue(jsonResult, "msg");
					}
				} else if ("".equals(result) || null == result) {
					errmessage = "不同意运输合同失败";
				} else {
					errmessage = "不同意运输合同失败\n" + result;
				}
			} catch (Exception e) {
				// TODO: handle exception
				errmessage = "不同意运输合同异常";
			}
			data.putString("msg", errmessage);
			data.putBoolean("isPassOrNot", checkPassOrNot);
			msg.setData(data);
			break;
		case HttpUrlConstant.CONFIRMLIST:
			try {
				Map<String, Object> params2 = new HashMap<String, Object>();
				params2.put("ship_id", sp.getShip_id());
				params2.put("page", 1);
				params2.put("size", 30);
				String url = HttpUrlConstant.SHIPBIZ_BASE_URL
						+ HttpUrlConstant.CONFIRMLIST_URL + "?";
				result = HttpUtil.queryStringForPost(url, params2, ctx)
						.toString();
				JSONObject obj = new JSONObject(result);
				JSONArray ary = obj.getJSONArray("content");
				if (ary.length() > 0) {
					data.putString("result", result);
					data.putString("submit_result", "isPass");
					data.putString("msg", "完成获取信息");
				} else {
					data.putString("submit_result", "noPass");
					data.putString("msg", "暂无确认签单信息");
				}
			} catch (Exception e) {
				// TODO: handle exception
				data.putString("submit_result", "noPass");
				data.putString("msg", "获取确认签单信息异常");
			}
			msg.setData(data);
			break;
		// 获取船东资料
		case HttpUrlConstant.INIT_CONTACT:
			try {
				SharePreferenceUtil spfu = new SharePreferenceUtil(ctx,
						HttpConstant.SAVE_USER);
				System.out.println(params);
				result = HttpUtil
						.queryStringForPost(
								HttpUrlConstant.BASE_URL
										+ HttpUrlConstant.INIT_CONTACT_URL,
								params, ctx).toString();
				System.out.println(result);
				JSONObject job = new JSONObject(result.toString());
				String getShipOwner = job.has("shipOwner") == false ? "" : job
						.get("shipOwner").toString();
				String getOwnerPhone = job.has("ownerPhone") == false ? ""
						: job.get("ownerPhone").toString();
				spfu.setShipOwnerName(getShipOwner);
				spfu.setShipOwnerPhone(getOwnerPhone);
				data.putBoolean("checkPassOrNot", true);
			} catch (Exception e) {
				// TODO: handle exception
			}
			msg.setData(data);
			break;
		// 提交船东资料
		case HttpUrlConstant.SHIP_OWNER:
			try {
				String url = HttpUrlConstant.BASE_URL
						+ HttpUrlConstant.SHIP_OWNER_URL;
				result = HttpUtil.queryStringForPost(url, params, ctx)
						.toString();
				if (!"".equals(result) && null != result) {
					JSONObject jsonResult = new JSONObject(result);
					if (jsonResult.getInt("rs") == 1) {
						errmessage = "提交资料成功";
						checkPassOrNot = true;
					} else if (jsonResult.getInt("rs") == 0) {
						errmessage = DatasTools.getJsonValue(jsonResult, "msg");
					}
				} else if ("".equals(result) || null == result) {
					errmessage = "提交资料失败";
				} else {
					errmessage = "提交资料失败\n" + result;
				}
			} catch (Exception e) {
				// TODO: handle exception
				errmessage = "提交资料异常";
			}
			data.putString("msg", errmessage);
			data.putBoolean("isPassOrNot", checkPassOrNot);
			msg.setData(data);
			break;
		// 检查版本更新
		case HttpUrlConstant.CHECK_VERSION_AND_UPDATE:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.CHECK_VERSION_AND_UPDATE_URL, params,
						ctx).toString();
				System.out.println(result);
				JSONObject getResult = new JSONObject(result.toString());
				if (result.length() > 0 && getResult.length() > 0) {
					String getVersionCode = getResult.has("versionCode") == false ? "1"
							: getResult.getString("versionCode");
					String getVersionName = getResult.has("versionName") == false ? "1.0"
							: getResult.getString("versionName");
					String getSize = getResult.has("size") == false ? "0.0"
							: getResult.getString("size");
					String getUrl = getResult.has("url") == false ? "null"
							: getResult.getString("url");
					String versionName = DatasTools.getVersion(ctx);
					double versionNameDb = Double.parseDouble(versionName);
					double getVersionNameInt = Double
							.parseDouble(getVersionName);
					int getVersionCodeInt = Integer.parseInt(getVersionCode);
					int localVersionCode = DatasTools.getVersionCode(ctx);
					if (getVersionCodeInt > localVersionCode
							|| getVersionNameInt > versionNameDb) {
						data.putString("url", getUrl);
						data.putString("size", getSize);
						data.putString("result", result.toString());
						data.putString(HttpConstant.OPA_TAG,
								HttpConstant.HAS_NEW_VERSION);
					} else {
						data.putString(HttpConstant.OPA_TAG,
								HttpConstant.HAS_NO_VERSION);
					}
				} else {
					data.putString(HttpConstant.OPA_TAG,
							HttpConstant.HAS_NO_VERSION);
				}
			} catch (Exception e) {
				data.putString(HttpConstant.OPA_TAG,
						HttpConstant.HAS_NO_VERSION);
				e.printStackTrace();
			}
			msg.setData(data);
			break;
		case HttpUrlConstant.CONFIRM_SIGN_INT:
			try {
				// 初始化缓存
				System.out.println(params);
				// param.put("eleManifestId", "86");
				result = HttpUtil
						.queryStringForPost(
								(HttpUrlConstant.SHIPBIZ_BASE_URL + HttpUrlConstant.CONFIRM_SIGN),
								params, ctx).toString();
				System.out.println(result);
				JSONObject joResult = new JSONObject(result.toString());
				if (joResult.has("aid")
						&& !"[]".equals(joResult.getString("aid"))) {
					data.putString("result", joResult.getString("aid"));
					data.putString("msg", "图片加载完成");
				} else {
					data.putString("msg", "没有图片信息");
					data.putString("result", "");
				}

			} catch (Exception e) {
				// TODO: handle exception
				data.putString("msg", "暂无图片加载");
				data.putString("result", "");
			}
			data.putString("jumpType", params.get("jumpType").toString());
			msg.setData(data);
			break;
		case HttpUrlConstant.SUBMIT_CONRIME_ORDER:
			try {
				// 初始化缓存
				System.out.println(params);
				// param.put("eleManifestId", "86");
				result = HttpUtil
						.queryStringForPost(
								(HttpUrlConstant.SHIPBIZ_BASE_URL + HttpUrlConstant.CONFIRM_SIGN),
								params, ctx).toString();
				System.out.println(result);
				JSONObject joResult = new JSONObject(result.toString());
				String id = joResult.getString("id");
				String subOrderId = joResult.getString("subOrderId");
				params.put("id", id);
				params.put("subOrderId", subOrderId);
				result = HttpUtil
						.queryStringForPost(
								(HttpUrlConstant.SHIPBIZ_BASE_URL + HttpUrlConstant.SUBMIT_CONRIME_ORDER_URL),
								params, ctx).toString();
				if (!"".equals(result) && null != result) {
					JSONObject jsonResult = new JSONObject(result);
					if (jsonResult.getInt("rs") == 1) {
						errmessage = "确认签单成功";
						checkPassOrNot = true;
					} else if (jsonResult.getInt("rs") == 0) {
						errmessage = DatasTools.getJsonValue(jsonResult, "msg");
					}
				} else if ("".equals(result) || null == result) {
					errmessage = "确认签单失败";
				} else {
					errmessage = "确认签单失败\n" + result;
				}
			} catch (Exception e) {
				// TODO: handle exception
				errmessage = "确认签单异常";
			}
			data.putString("msg", errmessage);
			data.putBoolean("isPassOrNot", checkPassOrNot);
			msg.setData(data);
			break;
		// 根据推送eleId获取相应数据
		case HttpUrlConstant.INIT_TOONEELE_BY_ID:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.INIT_TOONEELE_BY_ID_URI,
						params, ctx).toString();
				System.out.println(result);
				if (result.length() > 0) {
					data.putString("result", result);
					data.putString("submit_result", "isPass");
					data.putString("msg", "完成获取信息");
				} else {
					data.putString("submit_result", "noPass");
					data.putString("msg", "暂无运输合同信息");
				}
			} catch (Exception e) {
				// TODO: handle exception
				data.putString("msg", "运输合同信息获取异常");
			}
			msg.setData(data);
			break;
		// 根据id获取相应的货运申请信息
		case HttpUrlConstant.INIT_MAKETOORDER_BY_ID:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.INIT_MAKETOORDER_BY_ID_URI,
						params, ctx).toString();
				System.out.println(result);
				if (result.length() > 0) {
					data.putString("result", result);
					data.putString("submit_result", "isPass");
					data.putString("msg", "完成获取信息");
				} else {
					data.putString("submit_result", "noPass");
					data.putString("msg", "无法获取申请信息");
				}
			} catch (Exception e) {
				// TODO: handle exception
				data.putString("msg", "无法获取申请信息");
			}
			msg.setData(data);
			break;
		// 获取推送信息
		case HttpUrlConstant.GET_MESSAGE_LIST:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.GET_MESSAGE_LIST_URI, params,
						ctx).toString();
				System.out.println(result);
				JSONObject job = new JSONObject(result);
				if ("1".equals(job.getString("rs"))) {
					data.putString("result", result);
					data.putString("msg", "获取完成");
					data.putBoolean("ispass", true);
				} else if ("0".equals(job.getString("rs"))) {
					data.putString("msg", "服务器异常");
					data.putBoolean("ispass", false);
				}
			} catch (Exception e) {
				// TODO: handle exception
				data.putString("msg", "获取失败");
				data.putBoolean("ispass", false);

			}
			msg.setData(data);
			break;
		// 获取消息数量
		case HttpUrlConstant.GET_MESSAGE_COUNT:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.GET_MESSAGE_COUNT_URI,
						params, ctx).toString();
				System.out.println(result);
				JSONObject job = new JSONObject(result);
				if ("1".equals(job.getString("rs"))) {
					sp.setMessageCount(job.getString("count"));
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			data.putBoolean("ispass", true);
			msg.setData(data);
			break;
		case HttpUrlConstant.GET_MESSAGE_BY_KEY:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.GET_MESSAGE_BY_KEY_URI,
						params, ctx).toString();
				System.out.println(result);
				JSONObject job = new JSONObject(result);
				if ("1".equals(job.getString("rs"))) {
					sp.setMessageCount(job.getString("count"));
					data.putString("result", result);
					data.putString("msg", "获取完成");
					data.putBoolean("ispass", true);
				} else if ("0".equals(job.getString("rs"))) {
					data.putString("msg", "服务器异常");
					data.putBoolean("ispass", false);
				}
			} catch (Exception e) {
				// TODO: handle exception
				data.putString("msg", "获取失败");
				data.putBoolean("ispass", false);

			}
			msg.setData(data);
			break;
		case HttpUrlConstant.SUBMIT_MESSAGE_READED:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.SUBMIT_MESSAGE_READED_URI,
						params, ctx).toString();
				System.out.println(result);
				JSONObject job = new JSONObject(result);
				if ("1".equals(job.getString("rs"))) {
					data.putBoolean("ispass", true);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			data.putBoolean("ispass", false);
			msg.setData(data);
			break;
		// 申请取消运输合同
		case HttpUrlConstant.SUBMIT_REQUEST_CANCEL_ELE:
			try {
				System.out.println(params);
				String url = HttpUrlConstant.SHIPBIZ_BASE_URL
						+ HttpUrlConstant.SUBMIT_REQUEST_CANCEL_ELE_URI + "?";
				result = HttpUtil.queryStringForPost(url, params, ctx)
						.toString();
				if (!"".equals(result) && null != result) {
					JSONObject jsonResult = new JSONObject(result);
					if (jsonResult.getInt("rs") == 1) {
						if (jsonResult.has("msg")
								&& !"".equals(DatasTools.getJsonValue(
										jsonResult, "msg"))) {
							errmessage = DatasTools.getJsonValue(jsonResult,
									"msg");
						} else {
							errmessage = "申请取消运输合同成功";
						}
						checkPassOrNot = true;
					} else if (jsonResult.getInt("rs") == 0) {
						errmessage = DatasTools.getJsonValue(jsonResult, "msg");
					}
				} else if ("".equals(result) || null == result) {
					errmessage = "申请取消运输合同失败";
				} else {
					errmessage = "申请取消运输合同失败\n" + result;
				}
			} catch (Exception e) {
				// TODO: handle exception
				errmessage = "申请取消运输合同异常";
			}
			data.putString("msg", errmessage);
			data.putBoolean("isPassOrNot", checkPassOrNot);
			msg.setData(data);
			break;
		case HttpUrlConstant.GET_ELE_QUERY_RODE_COUNT:
			try {
				System.out.println(params);
				result = HttpUtil.queryStringForPost(
						HttpUrlConstant.SHIPBIZ_BASE_URL
								+ HttpUrlConstant.GET_ELE_QUERY_RODE_COUNT_URI,
						params, ctx).toString();
				System.out.println(result);
				JSONObject job = new JSONObject(result);
				if (job.has("redoCount")) {
					sp.setRedoCount(job.getString("redoCount"));
				}
			} catch (Exception e) {
			}
			msg.setData(data);
			break;
		default:
			System.out.println(params + result);
			break;
		}
		System.out.println("11--" + params + result);
		if ((result.indexOf("<html>") != -1 && result.indexOf("login") != -1)
				|| "没有登录或登录超时!".equals(data.getString("msg"))) {
			try {
				// 超时则跳转到登录界面
				ctx.startActivity(new Intent(ctx, LoginActivity.class));
			} catch (Exception e) {

			}
		}
		handler.sendMessage(msg);
	}
}
